package cn.net.isir.controller.manager;

import cn.net.isir.common.ResultObject;
import cn.net.isir.common.TokenManager;
import cn.net.isir.entity.dto.FinanceStatisticsDto;
import cn.net.isir.service2.FinanceStatisticsService;
import cn.net.isir.utils.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @author liuxubo
 * @filename FinanceStatisticsController.java
 * @package cn.net.isir.controller.manager
 * @email lxb@isir.net.cn
 * @date 2018/12/04
 * @comment 统计查询
 * @since 1.8
 */
@RestController
@RequestMapping("/statistics")
public class FinanceStatisticsController {
    @Resource
    private FinanceStatisticsService financeStatisticsService;


    /**
     * 根据订单查询
     *
     * @param request 请求
     * @return 查询结果
     */
    @RequestMapping(value = "/byOrder", method = RequestMethod.GET)
    public ResultObject statisticsByOrder(HttpServletRequest request) {
        return new ResultObject(200, "success",
                financeStatisticsService.statisticsByOrder(parseParam(request)));
    }

    /**
     * 根据品类查询
     *
     * @param request 请求
     * @return 查询结果
     */
    @RequestMapping(value = "/byCategory", method = RequestMethod.GET)
    public ResultObject statisticsByCategory(HttpServletRequest request) {
        return new ResultObject(200, "success",
                financeStatisticsService.statisticsByCategory(parseParam(request)));
    }

    /**
     * 根据菜品查询
     *
     * @param request 请求
     * @return 查询结果
     */
    @RequestMapping(value = "/byGoods", method = RequestMethod.GET)
    public ResultObject statisticsByGoods(HttpServletRequest request) {
        return new ResultObject(200, "success",
                financeStatisticsService.statisticsByGoods(parseParam(request)));
    }

    /**
     * 转换信息
     *
     * @param request 请求
     * @return 返回参数对象
     */
    private FinanceStatisticsDto parseParam(HttpServletRequest request) {

        FinanceStatisticsDto dto = new FinanceStatisticsDto();
        if (StringUtils.isNotBlank(request.getParameter("timeStart"))) {
            dto.setTimeStart(request.getParameter("timeStart"));
        }
        if (StringUtils.isNotBlank(request.getParameter("timeEnd"))) {
            dto.setTimeEnd(request.getParameter("timeEnd"));
        }
        if (StringUtils.isNotBlank(request.getParameter("shopId"))) {
            dto.setShopId(request.getParameter("shopId"));
        }
        if (StringUtils.isNotBlank(request.getParameter("pageNo"))) {
            dto.setPageNo(Integer.valueOf(request.getParameter("pageNo")));
        }
        if (StringUtils.isNotBlank(request.getParameter("pageSize"))) {
            dto.setPageSize(Integer.valueOf(request.getParameter("pageSize")));
        }
        dto.setUserId(TokenManager.getTokenId());
        return dto;
    }
}
